home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / astread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  6.9 KB  |  286 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "hdr.h"
  10. #include "vars.h"
  11. #include "attr.h"
  12. #include "ifile.h"
  13. #include "chapprots.h"
  14. #include "libfprots.h"
  15. #include "setprots.h"
  16. #include "sspansprots.h"
  17. #include "miscprots.h"
  18. #include "smiscprots.h"
  19. #include "astreadprots.h"
  20. /* procedures to read the ast */
  21. #define ZERO_NODE  9999
  22.  
  23. static Node astreadn(IFILE *);
  24. static void attrnum(Node);
  25.  
  26. Node astreadu(IFILE *astfile)                                    /*;astreadu*/
  27. {
  28.     /* read next unit from file astfile. Build all necessary
  29.      * nodes and return the root node of the unit. This can be done in one
  30.      * pass since all sub-ast and list nodes appear before their "parents".
  31.      * The global tuple seq_node maps node sequence nubmers to their locations
  32.      * and is used to relocate pointers upon reading in nodes. 
  33.      * seq_node_n refers to the last node allocated.
  34.      */
  35.  
  36.     Node    node, comp_unit;
  37.  
  38.     for (;;) {
  39.         node = astreadn(astfile); /* read next node */
  40.         if (N_KIND(node) == as_astend) return node;
  41.         if (N_KIND(node)== as_astnull) { /* if null node */
  42.             comp_unit = (Node) seq_node[seq_node_n];
  43.             return comp_unit;
  44.         }
  45.     }
  46. }
  47.  
  48. static Node astreadn(IFILE *astfile)                            /*;astreadn*/
  49. {
  50.     /* Return next node from ast file */
  51.     /* Return node with kind 0 for null node, kind as_astend for EOF */
  52.  
  53.     int    c;
  54.     int    n, astcount, listcount, val;
  55.     int    iv, count;
  56.     unsigned int kind;
  57.     short    span0, span1;
  58.     Tuple    list;
  59.     Node    newn, nod;
  60.     char    *newstr;
  61.  
  62.     count = getnum(astfile, "count");
  63.     if (count == -1) {
  64.         nod = node_new_noseq((unsigned) as_astend);
  65.         return nod;
  66.     }
  67.     if (count == 0) {
  68.         nod = node_new_noseq((unsigned) as_astnull);
  69.         return nod;
  70.     }
  71.  
  72.     /* Here at start of tuple */
  73.     kind = (unsigned) getnum(astfile, "kind");
  74.     /* allocate and initialize node */
  75.     if (count <= seq_node_n) {
  76.         /* copy of previously seen node, read in and ignore */
  77.         newn = node_new_noseq(kind);
  78.     }
  79.     else if (count == seq_node_n + 1) {  /* next node in sequence */
  80.         newn = node_new(kind);
  81.     }
  82.     else { /* unexpected node number */
  83.         chaos("astreadn: node out of sequence");
  84.     }
  85.     if (newn == (Node) 0) {
  86.         printf("astread cannot allocate node kind %d\n", kind);
  87.         chaos("astread - bad kind");
  88.     }
  89.     if (is_terminal_node(kind)) {
  90.         span0 = getnum(astfile, "span-line");
  91.         span1 = getnum(astfile, "span-col");
  92.     }
  93.     if (kind != as_line_no) {
  94.         /* as_line_no nodes do not have ast or list fields */
  95.         astcount = getnum(astfile, "astcount");
  96.         /* we need to distinguish the zero entered as initial value of AST
  97.          * field from the 0 used in input format to indicate OPT_NODE. Hence
  98.          * we mark nodes with ZERO_NODE, and then change values explicitly
  99.          * entered as zero to OPT_NODE
  100.          */
  101.         N_AST1(newn) = (Node)ZERO_NODE;
  102.         N_AST2(newn) = (Node)ZERO_NODE;
  103.         N_AST3(newn) = (Node)ZERO_NODE;
  104.         N_AST4(newn) = (Node)ZERO_NODE;
  105.         /* Now read ast count*/
  106.         for (n = 1; n <= astcount; n++) {
  107.             c = getnum(astfile, "ast-count");
  108.             if (c >= N_SEQ(newn))
  109.                 printf("astreadn: forward pointer in ast\n");
  110.             switch (n) {
  111.             case(1):    
  112.                 N_AST1(newn) = (Node) c; 
  113.                 break;
  114.             case(2):    
  115.                 N_AST2(newn) = (Node) c; 
  116.                 break;
  117.             case(3):    
  118.                 N_AST3(newn) = (Node) c; 
  119.                 break;
  120.             case(4):    
  121.                 N_AST4(newn) = (Node) c; 
  122.                 break;
  123.             default: 
  124.                 chaos("astreadn too many ast entries");
  125.             };
  126.         }
  127.         if (N_AST1(newn) == (Node)0) N_AST1(newn) = OPT_NODE;
  128.         else if (N_AST1(newn) != (Node)ZERO_NODE)
  129.             N_AST1(newn) = (Node) seq_node[(int)N_AST1(newn)];
  130.         else N_AST1(newn) = (Node)0;
  131.  
  132.         if (N_AST2(newn) == (Node)0) N_AST2(newn) = OPT_NODE;
  133.         else if (N_AST2(newn) != (Node)ZERO_NODE)
  134.             N_AST2(newn) = (Node) seq_node[(int)N_AST2(newn)];
  135.         else N_AST2(newn) = (Node)0;
  136.  
  137.         if (N_AST3(newn) == (Node)0) N_AST3(newn) = OPT_NODE;
  138.         else if (N_AST3(newn) != (Node)ZERO_NODE)
  139.             N_AST3(newn) = (Node) seq_node[(int)N_AST3(newn)];
  140.         else N_AST3(newn) = (Node)0;
  141.  
  142.         if (N_AST4(newn) == (Node) 0) N_AST4(newn) = OPT_NODE;
  143.         else if (N_AST4(newn) != (Node)ZERO_NODE)
  144.             N_AST4(newn) = (Node) seq_node[(int)N_AST4(newn)];
  145.         else N_AST4(newn) = (Node)0;
  146.  
  147.         /* convert attribute string to internal code */
  148.         if (N_KIND(newn) == as_attribute || N_KIND(newn) == as_range_attribute)
  149.             attrnum(newn);
  150.         /* now process list field */
  151.         listcount = getnum(astfile, "if-list");
  152.         if (listcount >= 0) {
  153.             list = tup_new(listcount);
  154.             for (n = 1; n <= listcount; n++) {
  155.                 val = getnum(astfile, "list-val");
  156.                 if (val >= N_SEQ(newn))
  157.                     printf("astreadn: forward pointer in ast\n");
  158.                 list[n] = (char *) seq_node[val];
  159.             }
  160.             N_LIST(newn) = list;
  161.         }
  162.  
  163.         iv = getnum(astfile, "iv");
  164.         if (iv == 0 ) goto set_span;
  165.  
  166.         newstr = getstr(astfile, "val");
  167.  
  168.         /* If mode, needing argument type conversion*/
  169.         if (N_KIND(newn) == as_mode) {
  170.             /* Input from tree has string. We convert to appropriate code
  171.              * according to the length of the string. This is sufficient, and
  172.              * avoids having to look at the characters in the string.
  173.              */
  174.             n = strlen(newstr);
  175.             switch(n) {
  176.             case (0):    
  177.                 N_VAL(newn) = (char * ) 0; 
  178.                 break;
  179.             case (2):    
  180.                 N_VAL(newn) = (char * ) na_in; 
  181.                 break;
  182.             case (3):    
  183.                 N_VAL(newn) = (char * ) na_out; 
  184.                 break;
  185.             case (5):    
  186.                 N_VAL(newn) = (char * ) na_inout; 
  187.                 break;
  188.             default:    
  189.                 printf(" node type as_mode invalid argument type %s\n", newstr);
  190.                 /* how to indicate node error - TBSL */
  191.             }
  192.         }
  193.         else {
  194.             N_VAL(newn) = newstr;
  195.         }
  196.     } /* end of processing "non as_line_no" nodes */
  197.     else {
  198.         /* here, handle as_line_no which has just one integer field */
  199.         c = getnum(astfile, "line-no");
  200.         N_VAL(newn) = (char *) c;
  201.     }
  202. set_span:
  203.     if (is_terminal_node(kind)) {
  204.         N_SPAN0(newn) = span0;
  205.         N_SPAN1(newn) = span1;
  206.     }
  207.     return newn;
  208. }
  209.  
  210. static void attrnum(Node node)                                        /*;attrnum*/
  211. {
  212.     /* convert attribute string to internal attribute code */
  213.     int        i;
  214.     char    *s, *name;
  215.     static char *attrnames[] = {
  216.         "ADDRESS",
  217.         "AFT",
  218.         "BASE",
  219.         "CALLABLE",
  220.         "CONSTRAINED",
  221.         "O_CONSTRAINED",
  222.         "T_CONSTRAINED",
  223.         "COUNT",
  224.         "DELTA",
  225.         "DIGITS",
  226.         "EMAX",
  227.         "EPSILON",
  228.         "FIRST",
  229.         "O_FIRST",
  230.         "T_FIRST",
  231.         "FIRST_BIT",
  232.         "FORE",
  233.         "IMAGE",
  234.         "LARGE",
  235.         "LAST",
  236.         "O_LAST",
  237.         "T_LAST",
  238.         "LAST_BIT",
  239.         "LENGTH",
  240.         "O_LENGTH",
  241.         "T_LENGTH",
  242.         "MACHINE_EMAX",
  243.         "MACHINE_EMIN",
  244.         "MACHINE_MANTISSA",
  245.         "MACHINE_OVERFLOWS",
  246.         "MACHINE_RADIX",
  247.         "MACHINE_ROUNDS",
  248.         "MANTISSA",
  249.         "POS",
  250.         "POSITION",
  251.         "PRED",
  252.         "RANGE",
  253.         "O_RANGE",
  254.         "T_RANGE",
  255.         "SAFE_EMAX",
  256.         "SAFE_LARGE",
  257.         "SAFE_SMALL",
  258.         "SIZE",
  259.         "O_SIZE",
  260.         "T_SIZE",
  261.         "SMALL",
  262.         "STORAGE_SIZE",
  263.         "SUCC",
  264.         "TERMINATED",
  265.         "VAL",
  266.         "VALUE",
  267.         "WIDTH",
  268.         0    };
  269.  
  270.     s = N_VAL(N_AST1(node));
  271.     for (i = 0;; i++) {
  272.         name = attrnames[i];
  273.         if (name == (char *)0) break;
  274.         if (streq(name, s)) {
  275.             /* on match, change ast1 node to hold number, not str*/
  276.             N_KIND(N_AST1(node)) = as_number;
  277.             N_VAL(N_AST1(node)) = (char *) i;
  278.             break;
  279.         }
  280.     }
  281. #ifdef IGNORE
  282.     printf("attribute %s\n", s);
  283.     chaos("unable to match attribute name");
  284. #endif
  285. }
  286.